'use strict';

var React = require('react-native');
var {
  IntentAndroid,
  StyleSheet,
  Text,
  TouchableNativeFeedback,
  AppRegistry,
  View,
  ToastAndroid,
} = React;

var OpenURLButton = React.createClass({

  propTypes: {
    url: React.PropTypes.string,
  },

  handleClick: function() {
    IntentAndroid.canOpenURL(this.props.url, (supported) => {
      //判断当前uri是否可以打开
      if (supported) {
        IntentAndroid.openURL(this.props.url);
      } else {
        ToastAndroid.show("不能识别当前uri",ToastAndroid.SHORT);
      }
    });
  },
  //返回当前button组件显示的视图，这里是一个TouchableNativeFeedback
  render: function() {
    return (
      <TouchableNativeFeedback
        onPress={this.handleClick}>
        <View style={styles.button}>
          <Text style={styles.text}>Open {this.props.url}</Text>
        </View>
      </TouchableNativeFeedback>
    );
  }
});

var styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: 'white',
    padding: 10,
    paddingTop: 30,
  },
  button: {
    padding: 10,
    backgroundColor: '#3B5998',
    marginBottom: 10,
  },
  text: {
    color: 'white',
  },
});

// 导出当前OpenURLButton组件
module.exports = OpenURLButton;